//
//  ProblemFeedBackViewController.m
//  FyApp
//
//  Created by user on 15/10/10.
//  Copyright © 2015年 user. All rights reserved.
//

#import "ProblemFeedBackViewController.h"
#import "sys/utsname.h"

@interface ProblemFeedBackViewController ()<UITextFieldDelegate,UITextViewDelegate,UIScrollViewDelegate>
{
    UITextView * _textView;
    UITextField *_nameLabelTF;
    
    UITextField *_contactTF;
    
    int keyboardhight;
    
    UIScrollView *_scrollView;
    
    UILabel *_placeholderLabel;
}
@property(nonatomic,strong)UIButton *sendBtn;

@end

@implementation ProblemFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = GRAYCOLOR;
    self.title = @"意见反馈";
    [self setUplayoutViews];
 
}

- (void)setUplayoutViews
{
    _scrollView  = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    _scrollView.contentSize = CGSizeMake(kWidth, kHeight);

    //弹出留言框
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(10.0f, 20.0f, kWidth-20.f, 180)];
    _textView.layer.borderColor = UIColor.grayColor.CGColor;
    _textView.layer.borderWidth = 1;
     _textView.layer.cornerRadius= 5;
    _textView.layer.masksToBounds = YES;
    _textView.font = [UIFont systemFontOfSize:14];
    _textView.textColor= [UIColor blackColor];
    _textView.delegate= self;
    _textView.layer.cornerRadius = 2;
    _textView.layer.borderWidth = 1.0f;
    _textView.layer.masksToBounds = YES;
    [_scrollView  addSubview:_textView];
    
    //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
    _placeholderLabel = [[UILabel alloc]init];
    _placeholderLabel.frame =CGRectMake(5, 2.5, kWidth-30, 40.0f);
    _placeholderLabel.text = @"请留下您的宝贵意见，我们将努力改进（360字以内）";
    _placeholderLabel.numberOfLines = 2;
    _placeholderLabel.textColor = [UIColor lightGrayColor];
    _placeholderLabel.adjustsFontSizeToFitWidth = YES;
    _placeholderLabel.font = [UIFont systemFontOfSize:14];
    _placeholderLabel.backgroundColor = [UIColor clearColor];
    [_textView addSubview:_placeholderLabel];
    
    _nameLabelTF = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_textView.frame)+20, kWidth - 20, 40.0f)];
    _nameLabelTF.delegate = self;
    _nameLabelTF.placeholder = @"请留下手机号码，方便我们与您取得联系";
    [_nameLabelTF setValue:[UIFont boldSystemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [_nameLabelTF setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    _nameLabelTF.keyboardType = UIKeyboardTypeNumberPad;
    _nameLabelTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _nameLabelTF.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"Mobile"];
    _nameLabelTF.borderStyle = UITextBorderStyleRoundedRect;
    _nameLabelTF.font = [UIFont systemFontOfSize:14.0f];
    _nameLabelTF.layer.cornerRadius = 2;
    _nameLabelTF.layer.borderWidth = 1.0f;
    _nameLabelTF.layer.masksToBounds = YES;
    [_scrollView addSubview:_nameLabelTF];
    
    

    if (!_sendBtn) {
        
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendBtn.frame  = CGRectMake(10.0f,kHeight-44 - 40, kWidth-32.0f, 44.0f);
        _sendBtn.backgroundColor = BLUECOLOR;
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sendBtn.layer.cornerRadius = 2;
        [_sendBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchUpInside];
         [_scrollView addSubview:_sendBtn];
    }

   

}

//提交按钮
-(void)send:(UIButton*)btn
{
    if (_textView.text.length <= 0)
    {
        [MBProgressHUD showError:@"请输入您宝贵的意见"];
        return;
    }
    if (_textView.text.length > 360)
    {
        [MBProgressHUD showError:@"反馈意见字数不能超过360字"];
        return;
    }
    if (![ZMTool validateTel:_nameLabelTF.text])
    {
        [MBProgressHUD showError:@"请输入正确的手机号"];
        return;
    }
    NSString *timestamp=[GeneralTool gettimestamp];
    
//    NSDictionary *dataDic=@{@"AddressId":@"0",
//                            @"UserCode":GETUSERCODE,
//                            @"ReceiveName":self.name.text,
//                            @"Mobile":self.phoneNumber.text,
//                            @"AreaCode":_data.Areacode,
//                            @"LocateAddress":areaAddress,
//                            @"Address":cityAddress,
//                            @"Longitude":Longitude,
//                            @"Latitude":Latitude,
//                            @"IsDefault":@"0",
//                            @"Sex":_sex,
//                            @"DetailAddress":self.doorNumber.text,
//                            @"Timestamp":timeStamp,
//                            @"Sign":sign};
//    NSDictionary *requestDic=@{@"m_UserAddr":dataDic};
    NSDictionary * dataDic = @{@"UserCode":GETUSERCODE,@"Mobile":_nameLabelTF.text,@"Info":_textView.text,@"Timestamp":timestamp};
    NSString * sign = [GeneralTool GetSin:dataDic];
    NSMutableDictionary * requestDic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    [requestDic setObject:sign forKey:@"Sigin"];
    [RequestTool requestDataWithType:@"post" andUrlString:@"User/Feedback" andParameters:requestDic andRequestFinished:^(id responses, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        if ([responses[@"IsSuccess"]intValue]==1) {
            [MBProgressHUD showSuccess:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else
        {
            [MBProgressHUD showError:@"提交意见失败"];
        }
        
    }];

}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *a = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (a.length>11) {
        
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_contactTF resignFirstResponder];
    [_nameLabelTF resignFirstResponder];
    return YES;
}
#pragma mark-textField方法





#pragma mark-关闭键盘(TextView) 换行时。隐藏键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView
{
    NSString *toBeString = textView.text;
    NSString * lang=[[[UIApplication sharedApplication]textInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 360) {
                textView.text = [toBeString substringToIndex:360];
            }
        }
    }
    else{
        if (toBeString.length > 360) {
            textView.text = [toBeString substringToIndex:360];
        }
    }

    if (textView.text.length == 0) {
        _placeholderLabel.text = @"请留下您的宝贵意见，我们将努力改进（360字以内）";
    }else{
        _placeholderLabel.text = @"";
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
